// pages/asc/myAscOrder/myAscOrder.js.
var _http = require('../../../utils/httpUtils.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        payTabsId: 1,
        totalCount: "",
        pageNum:1,//页码
        orderList:  [],
        hasorder:true,
        // [{
        //     "img": "/images/common/img4.png",
        //     "title": "History of Columbia University Korean historiography",
        //     "price": "1200.00",
        //     "name": "William Kirby",
        //     "payStatus": 1
        // }, {
        //     "img": "/images/common/img4.png",
        //     "title": "History of Columbia University Korean historiography",
        //     "price": "1600.00",
        //     "name": "William Kirby",
        //     "payStatus": 2
        // }, {
        //     "img": "/images/common/img4.png",
        //     "title": "History of Columbia University Korean historiography",
        //     "price": "3200.00",
        //     "name": "William Kirby",
        //     "payStatus": 1
        // }, {
        //     "img": "/images/common/img4.png",
        //     "title": "History of Columbia University Korean historiography",
        //     "price": "48200.00",
        //     "name": "William Kirby",
        //     "payStatus": 2
        // }],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        //从我的页面过来的订单类型
        let typeOptions = options.type;
        if (typeOptions) {
            this.setData({
                payTabsId: typeOptions,
                orderList:[]
            })
        }

        //获取订单列表   
        getOrderList(this);
    },
    //切换订单类型数据
    choostPayTabs: function (e) {
        let id = e.currentTarget.dataset.id;
        this.setData({
            payTabsId: id,
            pageNum:1,
            orderList:[]
        })
        getOrderList(this);
    },
    goPay:function(e){
        let orderno = e.currentTarget.dataset.orderno;
        console.log(orderno);
        wx.navigateTo({
            url: '../kechengPay/kechengPay?order_no='+orderno,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    kcxq:function(e){
        let order =  e.currentTarget.dataset.da;
        if(order.pay_status != 1){
            wx.navigateTo({
                url: '../kechengPay/kechengPay?order_no='+order.order_no,
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
            })
        }else{
            let cid = order.course[0].course_id;
            wx.navigateTo({
                url: '../kechenginfo/kechenginfo?id='+cid+'&payStatus='+order.pay_status,
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
            })
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            pageNum:1,
            orderList:[]
        })
        getOrderList(this);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.totalCount != 0){
            this.setData({
                pageNum :this.data.pageNum+1
            })
            getOrderList(this);
        }
       
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
/**
 * 查询订单列表
 * @param {*} _this 
 */
function getOrderList(_this) {
    console.log("订单页面有疑问,怎么判断是最后一页数据了?");
    let map = {};
    map.page = _this.data.pageNum;
    map.list_row = 10;
    
    switch (Number(_this.data.payTabsId)) {
        case 2:
            map.pay_status = 0;
            break;
        case 3:
            map.pay_status = 1;
            break;
        default:
            map.pay_status = 2;
            break;
    }

    _http.post(_http.orderList, map, function (res) {
        console.log(res);
        var hasorder = false;
        if(_this.data.pageNum == 1  && res.data.list.length==0){
            hasorder = false;
        }else{
            hasorder = true;
        }
        _this.setData({
            orderList:_this.data.orderList.concat(res.data.list),
            hasorder : hasorder,
        })
    })
}